/*
 * Adminica UI
 *
 * Copyright (c) 2010 Tricycle Interactive
 *
 * http://www.tricycle.ie
 *
 * This file configures all the different jQuery scripts used in the Adminica Admin template. Links to the scripts can be found at the beginning of each function
 *
 */

/**
 * Add new category
 */
function addCategory(parentId, targetId)
{
    $("#add-category-box").dialog({
        modal: true,
        buttons: {
            "Thêm": function() {
                var cat_name = $('#cat_name').val();
                var cat_info = $('#cat_info').val();
                
                //Check cat_name
                if(cat_name == ''){
                    $('#validateTips').html('<img width="24" height="24" src="/static/backend/images/icons/small/white/Alarm%20Bell.png">Vui lòng nhập tên chuyên mục!').fadeIn();
                    return false;
                }
                
                //Call Ajax to create new category
                $.ajax({
                    type: 'POST',
                    url: base_url + 'backend/categories/create_ajax',
                    data: {
                        parentId : parentId,
                        name : cat_name,
                        info : cat_info
                    },
                    dataType: 'json',
                    success: function(response){
                        if(response.error == 0){
                           $("#add-category-box").dialog("close");
                            $('#'+targetId).append('<option selected="selected" value="'+response.id+'">'+cat_name+'</option>');
                            $.uniform.update('#'+targetId);
                            //Reset form
                            $('#cat_name').val('');
                            $('#cat_info').val('');
                        }else{
                            $('#validateTips').html('<img width="24" height="24" src="/static/backend/images/icons/small/white/Alarm%20Bell.png">'+response.message).fadeIn();
                        }
                    }
                });
                
                //$(this).dialog("close");
            },
            "Hủy": function() {
                $(this).dialog("close");
            }
        }
    });
    
    return false;
}
 
$(function() {

    // Sidenav Accordion Config
    $( "ul#accordion" ).accordion({
        collapsible: true,
        active:false,
        header: 'li a.top_level',
        autoHeight:false,
        icons:false
    });
    
    // Uniform Config (more info can found at http://pixelmatrixdesign.com/uniform/)
    $( "select, input:checkbox, input:radio, input:file").uniform();
    
//    // Tipsy Top Config (more info found at http://onehackoranother.com/projects/jquery/tipsy/)
//    $('[title]').tipsy({
//        fade: true,     // fade tooltips in/out?
//        fallback: '',    // fallback text to use when no tooltip text
//        gravity: 's',    // gravity
//        opacity: 1,    // opacity of tooltip
//        title: 'title',  // attribute/callback containing tooltip text
//        trigger: 'hover' // how tooltip is triggered - hover | focus | manual    	
//    });
    
    // Tipsy Side Config
    $('input[title]').tipsy({
        trigger: 'focus',  
        offset:'5',
        gravity: 'w'
    });
    
    // Dismiss alert box
    $(".alert").click(function(){
        $(this).fadeOut('slow');
    });
});
